<?php

namespace App\Tools\Service;

use app\aliyun\controller\AliyunFilterWord;
use app\aliyun\controller\AliyunFilterWordV2;
use App\Models\CacheModel\ai_chat_room\AiChatTag;
use Log;

class AiChatRoomService
{

    /**
     * 参数 校验
     * @param array $data
     * @return array
     */
    public static function paramCheck(array $data, array $configData)
    {
        if (mb_strlen($data['name']) > $configData['name_max_length'])
        {   // 昵称超过最大长度
            return ['status' => false, 'msg' => '名字最多5个字',];
        }
        if (empty($data['avatar']))
        {
            return ['status' => false, 'msg' => '请上传头像',];
        }
        if (mb_strlen($data['desc']) > $configData['type_list'][$data['type']]['desc_max_length'])
        {   // 陪聊对象的要求超过最大长度
            return ['status' => false, 'msg' => '陪聊对象的要求最多' . $configData['type_list'][$data['type']]['desc_max_length'] . '个字',];
        }
        //非系统图片检测
        if (strpos($data['avatar'], '/test') === false)
        {
            $oss = Env::get('host.photo');
            $aliRes = AliyunFilterWord::checkImg(1, $oss . $data['avatar'], 'ai_peililao', ['terrorism']);
            if ($aliRes['status'] === false)
            {
                return ['status' => false, 'msg' => '头像违规'];
            }
        }

        $aliRes = AliyunFilterWord::checkText($data['name'], 'ai_peililao');
        if ($aliRes)
        {
            return ['status' => false, 'msg' => '名称涉嫌' . $aliRes['label']];
        }
        if ($data['desc'])
        {
            $aliRes = AliyunFilterWordV2::checkAIText($data['desc']);
            if ($aliRes['status'] === false)
            {
                return ['status' => false, 'msg' => '检测到敏感内容'];
            }
        }

        $optionData = (new AiChatTag())->getChatTagData($data['type']);

        $resConfig = [];
        foreach ($optionData as $optionDatum)
        {
            if (!isset($data['config'][$optionDatum['key']]))
            {
                return ['status' => false, 'msg' => $optionDatum['title'] . '未知',];
            }
            $options = explode(',', $data['config'][$optionDatum['key']]);
            $options = array_map('intval', $options);
            if (count($options) > $optionDatum['max_select_num'])
            {
                return ['status' => false, 'msg' => $optionDatum['title'] . '最多选' . $optionDatum['max_select_num'] . '项',];
            }
            foreach ($options as $optionId)
            {
                if (!in_array($optionId, $optionDatum['option_ids'], true))
                {
                    return ['status' => false, 'msg' => $optionDatum['title'] . '信息错误',];
                }
            }
            $resConfig[$optionDatum['key']] = [
                'tid' => $optionDatum['id'],
                'ids' => $options,
            ];
            if ($optionDatum['max_select_num'] === 1)
            {   // 单选 判断是否有子集
                foreach ($optionDatum['option_list'] as $item)
                {
                    if ($item['id'] === $options[0])
                    {
                        if ($item['node'])
                        {
                            $nodeData = $item['node'];
                            if (!isset($data['config'][$nodeData['key']]))
                            {
                                return ['status' => false, 'msg' => $nodeData['title'] . '未知',];
                            }

                            $options2 = explode(',', $data['config'][$nodeData['key']]);
                            $options2 = array_map('intval', $options2);
                            if (count($options2) > $nodeData['max_select_num'])
                            {
                                return ['status' => false, 'msg' => $nodeData['title'] . '最多选' . $nodeData['max_select_num'] . '项',];
                            }
                            foreach ($options2 as $optionId)
                            {
                                if (!in_array($optionId, $nodeData['option_ids'], true))
                                {
                                    return ['status' => false, 'msg' => $nodeData['title'] . '信息错误',];
                                }
                            }
                            $resConfig[$nodeData['key']] = [
                                'tid' => $nodeData['id'],
                                'ids' => $options2,
                            ];
                        }
                        break;
                    }
                }
            }
        }

        return [
            'status' => true,
            'msg' => '',
            'data' => $resConfig,
        ];
    }
}